import { AfterViewInit, Component, Injector, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { Subject } from 'rxjs';
import { listAnimation } from '../../../../animations/list.animation';
import { BaseUI } from '../../../../common/base';
import { JobService } from '../service/job.service';
import { TableComponent } from '../../../../components/table/table.component';
import { ColumnType } from '../../../../components/table/enums/ColumnType';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../../../components/table/table-option/table-option.model';
import { DateUtil } from '../../../../utils/DateUtil';
import { GlobalConfig } from '../../../../common/GlobalConfig';
import { JobOptionComponent } from '../component/job-option/job-option.component';
import { RunOptionComponent } from '../component/run-option/run-option.component';
import { JobDetailComponent } from '../component/job-detail-option/job-detail.component';
import { jobLogPage } from '../../../../app-routing.module';

@Component({
    selector: 'app-job',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss'],
    animations: [listAnimation]
})
export class JobComponent extends BaseUI implements OnInit, AfterViewInit, OnDestroy {
    onDestroy$: Subject<any> = new Subject<any>();

    constructor(
        public _config: ConfigModule,
        private _injector: Injector,
        public mainService: JobService,
    ) {
        super(_injector);
    }

    ngOnInit() {
    }

    ngAfterViewInit() {}

    ngOnDestroy() {
        this.onDestroy$.complete();
    }
    /**
     * 表格相关
     */

    @ViewChild("mainTable") mainTable: TableComponent;
    searchObj:any = {
        pageNum: 1,
        deptId: '',
        pageSize: this._config.pageSize,
        name: '',
        phonenumber: '',
        emailNumber: '',
        status: "",
    };

    columnArray = [
        {type: ColumnType.TEXT, name: '任务编号', key: 'jobId'},
        {type: ColumnType.TEXT, name: '任务名称', key: 'jobName'},
        {type: ColumnType.TEXT, name: '任务组名', key: 'jobGroup'},
        {type: ColumnType.TEXT, name: '调用目标字符串', key: 'invokeTarget'},
        {type: ColumnType.TEXT, name: 'cron执行表达式', key: 'cronExpression'},
        {type: ColumnType.SWITCH, name: '状态', key: 'status'},
    ];
    optionModelList: TableOptionModel[] = [
        {
        optionLabel:"修改",
        optionLabelType: OptionTypeEnum.TEXT,
        eventType:"",
        eventKey:EventKeyEnum.EDIT
        },
        {
            optionLabel:"删除",
            optionLabelType: OptionTypeEnum.TEXT,
            eventType:"",
            eventKey:EventKeyEnum.DELETE
        },
        {
            optionLabel:"更多",
            optionLabelType: OptionTypeEnum.MORE,
            eventType:"",
            children:[
                {
                optionLabel:"执行一次",
                optionLabelType: OptionTypeEnum.TEXT,
                eventKey:EventKeyEnum.DO_ONE
                },
                {
                    optionLabel:"任务详情",
                    optionLabelType: OptionTypeEnum.TEXT,
                    eventKey:EventKeyEnum.DETAIL
                },
                {
                    optionLabel:"调度日志",
                    optionLabelType: OptionTypeEnum.TEXT,
                    eventKey:EventKeyEnum.LOG
                },
            ]

        }
    ];
    otherOptionModelList: TableOptionModel[] = null;

    globalCaseMap = GlobalConfig.globalCaseMap;

    optionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.EDIT:
                this.optionComponent.onModalOpen(data.jobId);
                break;
            case EventKeyEnum.ADD:
                this.optionComponent.onModalOpen(null);
                break;
            case EventKeyEnum.DO_ONE:
                this.runOptionComponent.onModalOpen(data.jobId,data.jobGroup,data.jobName);
                break;
            case EventKeyEnum.DETAIL:
                this.mainService.get(data.jobId).subscribe(res => {
                    this.jobDetailComponent.onModalOpen(res.data);
                })
                break;
            case EventKeyEnum.LOG:
                this.router.navigate(['/manage/'+jobLogPage.path,data.jobId]).finally();
                break;
        }
    }

    searchList() {
        this.mainTable.list();
    }

    keyEnter($event: KeyboardEvent) {
        if($event.key === 'Enter'){
            this.searchList();
        }
    }


    /**
     * 日期选择
     */

    datePick: string[]=[];
    onDatePick() {
        this.searchObj.params = {};
        this.searchObj.params['beginTime'] = DateUtil.formatDate(this.datePick[0],"yyyy-MM-dd");
        this.searchObj.params['endTime'] = DateUtil.formatDate(this.datePick[1],"yyyy-MM-dd");
    }

    /**
     * 弹窗相关
     */
    @ViewChild('optionComponent') optionComponent: JobOptionComponent;


    resetSearchObject(){
        this.searchObj = {
            pageNum: 1,
            pageSize: this._config.pageSize,
        };
        this.datePick=[];
    }

    @ViewChild('runOptionComponent') runOptionComponent: RunOptionComponent
    @ViewChild('jobDetailComponent') jobDetailComponent: JobDetailComponent
}
